# -*- coding:utf-8 -*-

import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import threading
import hashlib
import urllib.parse
import time
import timedelta
import datetime
import requests
from common.my_time import MyTime
from common.attr_v3 import AttrV3
from database.db_handler import MysqlHander
from common.entity_data import EntityData
import json
import os
from selenium import webdriver

from webdriver_base import WebdriverBase
from common.my_file import MyFile


class ZuqiuliansaiData(WebdriverBase):
    def __init__(self):
        self.entity = EntityData()
        WebdriverBase.__init__(self, './log/' + os.path.split(__file__)[-1].split(".")[0] + ".log")
        self.qiudui = []
        self.all_team_member = ""
        self.desc = "包含:"

    def __del__(self):
        pass

    def query_qiuyuan_data(self, url):
        print(url)
        self.driver.get(url)
        try:
            entity_name = self.driver.title.split("|")[0].split("_")[1].replace("赛程","")
            tables = self.driver.find_elements_by_tag_name("table")
            for table in tables:
                th_name = table.find_element_by_tag_name("thead").find_elements_by_tag_name("th")[1].text
                if th_name != "姓名":
                    print(th_name)
                    continue

                name_list = ""
                for tr in table.find_element_by_tag_name("tbody").find_elements_by_tag_name("tr"):
                    name_val = tr.find_elements_by_tag_name("td")[1].text.split("·")
                    name_val2 = name_val[len(name_val) - 1].split(" ")
                    name_list += "|" + name_val2[len(name_val2) - 1]
                    self.all_team_member += "|" + name_val2[len(name_val2) - 1]
                    
                print(entity_name, name_list)
                if name_list == "":
                    return
                self.entity.add_entity_data(entity_name + "足球队阵容", "", 200, 2000003, entity_name, "", name_list)
                self.entity.update_entity_data(entity_name + "足球队阵容", "", 200, 2000003, entity_name, "", name_list)
                return
        except Exception as e:
            print(str(e))    

    def query_data(self, url):
        self.driver.get(url)
        try:
            trs = self.driver.find_element_by_class_name("small-block-grid-1").find_element_by_tag_name("tbody").find_elements_by_tag_name("tr")
            for tr in trs:
                name = tr.find_elements_by_tag_name("td")[2].find_element_by_tag_name("a").get_attribute("href")
                self.qiudui.append(name)
        except Exception as e:
            print(str(e))    
    
    def get_data(self):
        url_list = ["https://www.dszuqiu.com/league/876","https://www.dszuqiu.com/league/36","https://www.dszuqiu.com/league/38","https://www.dszuqiu.com/league/37","https://www.dszuqiu.com/league/39","https://www.dszuqiu.com/league/40","https://www.dszuqiu.com/league/49","https://www.dszuqiu.com/league/1253","https://www.dszuqiu.com/league/2608","https://www.dszuqiu.com/league/1058","https://www.dszuqiu.com/league/221","https://www.dszuqiu.com/league/204","https://www.dszuqiu.com/league/230","https://www.dszuqiu.com/league/1764","https://www.dszuqiu.com/league/2309","https://www.dszuqiu.com/league/943","https://www.dszuqiu.com/league/343","https://www.dszuqiu.com/league/2546","https://www.dszuqiu.com/league/251","https://www.dszuqiu.com/league/2307","https://www.dszuqiu.com/league/2893","https://www.dszuqiu.com/league/272","https://www.dszuqiu.com/league/1879","https://www.dszuqiu.com/league/664","https://www.dszuqiu.com/league/2440","https://www.dszuqiu.com/league/2432","https://www.dszuqiu.com/league/2276","https://www.dszuqiu.com/league/3310","https://www.dszuqiu.com/league/3287","https://www.dszuqiu.com/league/783","https://www.dszuqiu.com/league/2711","https://www.dszuqiu.com/league/1211","https://www.dszuqiu.com/league/35","https://www.dszuqiu.com/league/734","https://www.dszuqiu.com/league/952","https://www.dszuqiu.com/league/8","https://www.dszuqiu.com/league/2495","https://www.dszuqiu.com/league/108","https://www.dszuqiu.com/league/1835","https://www.dszuqiu.com/league/203","https://www.dszuqiu.com/league/158","https://www.dszuqiu.com/league/147","https://www.dszuqiu.com/league/201","https://www.dszuqiu.com/league/434","https://www.dszuqiu.com/league/104","https://www.dszuqiu.com/league/1807","https://www.dszuqiu.com/league/1840","https://www.dszuqiu.com/league/1362","https://www.dszuqiu.com/league/733","https://www.dszuqiu.com/league/106","https://www.dszuqiu.com/league/182","https://www.dszuqiu.com/league/101","https://www.dszuqiu.com/league/2435","https://www.dszuqiu.com/league/2437","https://www.dszuqiu.com/league/105","https://www.dszuqiu.com/league/151","https://www.dszuqiu.com/league/860","https://www.dszuqiu.com/league/2","https://www.dszuqiu.com/league/3342","https://www.dszuqiu.com/league/402","https://www.dszuqiu.com/league/2490","https://www.dszuqiu.com/league/316","https://www.dszuqiu.com/league/850","https://www.dszuqiu.com/league/390","https://www.dszuqiu.com/league/1670","https://www.dszuqiu.com/league/2334","https://www.dszuqiu.com/league/722","https://www.dszuqiu.com/league/2667","https://www.dszuqiu.com/league/559","https://www.dszuqiu.com/league/1123","https://www.dszuqiu.com/league/226","https://www.dszuqiu.com/league/32","https://www.dszuqiu.com/league/2920","https://www.dszuqiu.com/league/1606","https://www.dszuqiu.com/league/2970","https://www.dszuqiu.com/league/2660","https://www.dszuqiu.com/league/592","https://www.dszuqiu.com/league/857","https://www.dszuqiu.com/league/1908","https://www.dszuqiu.com/league/336","https://www.dszuqiu.com/league/2320","https://www.dszuqiu.com/league/1871","https://www.dszuqiu.com/league/2871","https://www.dszuqiu.com/league/2658","https://www.dszuqiu.com/league/2966","https://www.dszuqiu.com/league/1671","https://www.dszuqiu.com/league/1898","https://www.dszuqiu.com/league/959","https://www.dszuqiu.com/league/2528","https://www.dszuqiu.com/league/2285","https://www.dszuqiu.com/league/268","https://www.dszuqiu.com/league/598","https://www.dszuqiu.com/league/1325","https://www.dszuqiu.com/league/1199","https://www.dszuqiu.com/league/1690","https://www.dszuqiu.com/league/45","https://www.dszuqiu.com/league/2999","https://www.dszuqiu.com/league/2398","https://www.dszuqiu.com/league/3286","https://www.dszuqiu.com/league/1573","https://www.dszuqiu.com/league/3051","https://www.dszuqiu.com/league/1124","https://www.dszuqiu.com/league/1808","https://www.dszuqiu.com/league/1197","https://www.dszuqiu.com/league/3268","https://www.dszuqiu.com/league/3063","https://www.dszuqiu.com/league/2529","https://www.dszuqiu.com/league/375"]
        for url in url_list:
            self.query_data(url)
            for url_qiudui in self.qiudui:
                self.query_qiuyuan_data(url_qiudui)
            self.qiudui = []
        self.entity.add_entity_data("热门足球联赛球员", "", 200, 2000003, "足球", "", self.all_team_member)
        self.entity.update_entity_data("热门足球联赛球员", "", 200, 2000003, "足球", "", self.all_team_member)
        self.driver.quit()

if __name__ == '__main__':
    d = ZuqiuliansaiData()
    d.get_data()

